package com.baitiaojun.common.enums.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import com.baitiaojun.common.constants.HttpStatus;
import com.baitiaojun.common.enums.Enums;
import lombok.AllArgsConstructor;

import java.util.Arrays;

@AllArgsConstructor
public enum CommonStatusEnums implements Enums {

    SUCCESS(HttpStatus.SUCCESS, "成功"),
    FAIL(HttpStatus.ERROR, "失败");

    private final Integer code;

    private final String name;

    /**
     * 根据code判断是否成功
     * @param code
     * @return
     */
    private boolean isSuccess(Integer code) {
        return ObjUtil.equal(SUCCESS.code, code);
    }

    /**
     * 根据code判断是否失败
     * @param code
     * @return
     */
    private boolean isFail(Integer code) {
        return ObjUtil.equal(FAIL.code, code);
    }

    @Override
    public Enums valueOf(Integer code) {
        return ArrayUtil.firstMatch(item -> item.getCode().equals(code), values());
    }

    @Override
    public int[] array() {
        return Arrays.stream(values()).mapToInt(CommonStatusEnums::getCode).toArray();
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getName() {
        return name;
    }
}